package com.bytz.modules.cms.domain.manager;

import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.order.model.OrderItemModel;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.product.model.ProductModel;
import com.bytz.modules.cms.product.service.IMallProductService;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.reseller.model.ResellerSalesProductLine;
import com.bytz.modules.cms.reseller.service.IResellerSalesService;
import com.bytz.modules.cms.user.entity.CmsSysUser;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.OrderComparator;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 商城订单处理管理器
 *
 * @author shanxuezhong
 */
public class MallOrderProcessorManager {
    /**
     * 处理器
     */
    private final LinkedList<MallOrderProcessor> processors;

    private IResellerSalesService resellerSalesService;

    @Autowired
    public void setResellerSalesService(IResellerSalesService resellerSalesService) {
        this.resellerSalesService = resellerSalesService;
    }


    private IMallProductService mallProductService;

    @Autowired
    public void setMallProductService(IMallProductService mallProductService) {
        this.mallProductService = mallProductService;
    }


    public MallOrderProcessorManager(List<? extends MallOrderProcessor> processors) {
        this.processors = processors.stream().sorted(OrderComparator.INSTANCE).collect(Collectors.toCollection(LinkedList::new));
    }



    /**
     * 进行处理
     */
    public MallOrderProcessContext process(OrderModel order, String resellerId, boolean submit) {

        MallOrderProcessContext context = createContext(order, resellerId, submit);


        for (MallOrderProcessor processor : processors) {
            processor.doProcess(context);
        }

        return context;
    }

    @NotNull
    private MallOrderProcessContext createContext(OrderModel order, String resellerId, boolean submit) {
        MallOrderProcessContext context = new MallOrderProcessContext();
        context.setOrder(order);
        context.setResellerId(resellerId);
        context.setSubmit(submit);
        ResellerSalesProductLine salesAndProductLine = resellerSalesService.getSalesAndProductLine(order.getSalesId(), resellerId);
        if (salesAndProductLine != null) {
            CmsSysUser seller = salesAndProductLine.getSeller();
            if (seller != null) {
                context.setSeller(seller);
            }
            ProductLine productLine = salesAndProductLine.getProductLine();
            if (productLine != null) {
                context.setSellerProductLine(productLine);
            }

        } else {
            if (submit) {
                throw new BytzBootException("找不到订单对应产品线");
            }
        }
        List<OrderItemModel> orderItemList = order.getOrderItemList();
        if (CollectionUtils.isEmpty(orderItemList)) {
            throw new BytzBootException("找不到订单商品");
        }
        Set<String> productNums = orderItemList.stream().map(OrderItemModel::getProductNumber).collect(Collectors.toSet());
        List<ProductModel> productModels = mallProductService.getProductDetailByNumbers(productNums);
        context.setProductModel(productModels);
        return context;
    }
}